package show.me.the.money.util.exp.standard;

import java.util.List;

import show.me.the.money.util.context.Context;
import show.me.the.money.util.exp.ExpRunner;
import show.me.the.money.util.exp.ExpSymbols;
import show.me.the.money.util.exp.Expression;

@SuppressWarnings("unchecked")
public class SUBSTR extends Expression {

	public SUBSTR() {
		symbol = ExpSymbols.SUBSTR;
	}

	public String run(List lsExp, Context ctx) {
		try {
			List la1 = (List)lsExp.get(1);
			String s = (String)ExpRunner.run(la1, ctx);

			int start = (Integer)lsExp.get(2);
			int end  = (Integer)lsExp.get(3);

			return s.substring(start, end);
		} 
		catch (Exception e) {
			logger.fatal("run JSONException:", e);
		}
		return "";
	}

	public String toString(List lsExp, Context ctx) {
		try {
			List la1 = (List)lsExp.get(1);
			String s = (String)ExpRunner.run(la1, ctx);
			int start = (Integer)lsExp.get(2);
			int end  = (Integer)lsExp.get(3);
			return symbol + "(" + s + "," + start + "," + end + ")";
		} 
		catch (Exception e) {
			logger.fatal("run JSONException:", e);
		}
		return symbol + "()";
	}
}
